﻿<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ include file="../../common/jsp/taglibs.jsp"%>
<!-- 
    Copyright (C)  2011  chen wei hua.
    Permission is granted to copy, distribute and/or modify this document
    under the terms of the GNU Free Documentation License, Version 2.0
    or any later version published by the Free Software Foundation;
    with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts.
    A copy of the license is included in the section entitled "GNU Free Documentation License".
 -->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML xmlns="http://www.w3.org/1999/xhtml"><HEAD><TITLE>生产计划-新建月度计划</TITLE>
<META content="text/html; charset=utf-8" http-equiv=Content-Type>
<%@ include file="../../common/jsp/config.jsp"%>
<%@ include file="../../common/jsp/validate.jsp"%>
<%@ include file="../../common/jsp/autocomplete.jsp"%>
<script type="text/javascript">
var rowIndex = <s:property value="planDetails.size"/> - 1;
function addLine() {
	rowIndex++;
	var table = $("#planDetailList");
	table.find("tr").last().before(
	           '<tr class="odd">'
	          +'<td><input type="button" value="请选择" onclick="openSelectDialog('+rowIndex+')" />'
	          +'<s:hidden id="productNos-'+rowIndex+'" name="productNos" />'
	          +'<s:hidden id="productIds-'+rowIndex+'" name="productIds" /></td>'
	          +'<td><span id="productRootCategorys-'+rowIndex+'"></span>'
	          +'<s:hidden id="rootCategoryNames-'+rowIndex+'" name="rootCategoryNames" /></td>'
	          +'<td><span id="productCategorys-'+rowIndex+'"></span>'
	          +'<s:hidden id="categoryNames-'+rowIndex+'" name="categoryNames" /></td>'
	          +'<td><span id="productNames-'+rowIndex+'"></span></td>'
	          +'<td><span id="productTypes-'+rowIndex+'"></span></td>'
	          +'<td><span id="productSkuNames-'+rowIndex+'"></span>'
	          +'<s:hidden id="productSkus-'+rowIndex+'" name="productSkus" /></td>'
	          +'<td><span id="productUnits-'+rowIndex+'"></span></td>'
	          +'<td><span id="acceptNumbers-'+rowIndex+'"></span></td>'
	          +'<td><s:textfield id="planNums-'+rowIndex+'" name="planNums" size="10" cssClass="{required:true,maxlength:10,number:true}"/></td>'
	          +'<td><s:textfield id="batchNos-'+rowIndex+'" name="batchNos" size="10" cssClass="{required:true,maxlength:10}"/></td>'
	          +'<td></td>'
	          +'<td></td>'
	          +'<td></td>'
	          +'<td><input type="button" value="删除" class="input1" onclick="deleteRow(this)" /></td>'
	          +'</tr>');
}

function deleteRow(button) {
	$(button).parents("tr").remove();
}

function openSelectDialog(id) {
 	alertDialog(700, 500, "showSelectMaterialsDialog.action", 
 	function (result) {
 		var materials = result.materials;
 		if (materials) {
 			var fieldNames = "";
 			var skuId = result.skuId;
 		   	var skuConfig = materials.skuDetails;
 		   	var acceptNumber = 0;
 	 	   	for (var i in skuConfig) {
 	 	 	   	var config = skuConfig[i];
 	 	 	   	if (config.id == skuId) {
 	 	 	   		acceptNumber = config.number;
 	 	 	 	 	for (var j in config.fields) {
 	  	   				var field = config.fields[j];
 	  	   				fieldNames += field.customName + " ";
 	 	 	 	 	}
 	 	 	 	 	break;
 	 	 	   	}
 	 	   	}
 	 	   	$("#productNos-" + id).val(materials.code);
 	 	    $("#productIds-" + id).val(skuId);
 			$("#productRootCategorys-" + id).html(materials.rootCategory.name);
 			$("#rootCategoryNames-" + id).val(materials.rootCategory.name);
 			$("#productCategorys-" + id).html(materials.category.name);
 			$("#categoryNames-" + id).val(materials.category.name);
 			$("#productNames-" + id).html(materials.name);
 			$("#productTypes-" + id).html(materials.type);
 			$("#productSkuNames-" + id).html(fieldNames);
 			$("#productSkus-" + id).val(fieldNames);
 			$("#productUnits-" + id).html(materials.units);
 			$("#acceptNumbers-" + id).html(acceptNumber);
 		}
 	});
}

function saveSubmit() {
	var isEmpty = false;
	var allIsCurrent = true;
	$("input[id^='planNums-']").each(function(i) {
		if (isEmpty) 
			return;
		if ("" == $(this).val()) {
			isEmpty = true;
		}
		
		if (!allIsCurrent) 
			return;
		var planNumber = parseFloat($(this).val());
		if (planNumber > 0) {
			var id = this.id;
			var batchNoId = id.replace("planNums-", "batchNos-");
			var batchNo = $("#" + batchNoId).val();
			if (batchNo == "") {
				allIsCurrent = false;
			}
		}
	});
	if (isEmpty) {
		alert("计划数量必须填写，无需计划的可以写0!");
		return false;
	}
	if (!allIsCurrent) {
		alert("计划数量大于0的计划必须填写批号!");
		return false;
	}
	//alert("submit success");
	$("#saveMonthPlan").submit();
	return false;
}

$(document).ready(function() {
	$("#saveMonthPlan").validate();
	$("#planDetailList tr").length - 2;
});
</script>
</HEAD>
<BODY>
    <s:form action="saveMonthPlan">
    <s:hidden name="queryDate"/>
    <div class="subnav_long" align="left">
           <table width="100%" border="0" cellspacing="0" cellpadding="0">
		  <tr>
			<td width="100%" align="left" height="31">&nbsp;<img src="common/images/title-pk.gif" border="0"  align="absmiddle"/>&nbsp;<span class="subnavtxt1">生产计划</span>
			&nbsp;<img src="common/images/leftico2.gif" border="0"  align="absmiddle"/>&nbsp;<span class="subnavtxt2">添加月度计划</span></td><!-- <s:property value="@com.javaeye.common.util.DateUtils@stringToToday('yyyy-MM')" /> -->
		  </tr>
		</table>
      </div>
     <div class="main_1">
       <table width="99%" border="0" cellpadding="5" cellspacing="1" bgcolor="#D3DEEF">
         <tr>
           <td align="left" class="Blue14" bgcolor="#FAFAFA"><s:property value="queryDate" />月份计划</td>
         </tr>
       </table>
     </div>
     <div class="clear"></div>
	 <div class="main_scroll">
       <table width="1300px" cellpadding="0" cellspacing="1" border="0" class="ksglxx2a" id="planDetailList">
        <tr class="ksglxx2bg">
          <th width="60px" >&nbsp;</th>
          <th width="50px" >大分类</th>
          <th width="60px" >中分类</th>
          <th width="95px" >名称</th>
          <th width="180px" >型号</th>
          <th width="180px" >其他</th>
		  <th width="50px">单位</th>
		  <th width="65px" >仓库数量</th>
		  <th width="95px" >计划数量<span class="red-text1">*</span></th>
		  <th width="95px" >生产批号<span class="red-text1">*</span></th>
		  <th width="95px" >出货单号</th>
		  <th width="65px" >出货数量</th>
		  <th width="85px" >完成日期</th>
		  <th align="left"></th>
		</tr>
		<s:iterator value="planDetails" id="detail" status="st">
	      <s:iterator value="#detail.orderInfos" id="order" status="ost">
	        <s:if test="#st.Odd">
	          <tr class="odd">
	        </s:if>
	        <s:else>
	          <tr class="even">
	        </s:else>
	        <s:if test="#ost.index==0">
	          <td rowspan='<s:property value="#detail.orderInfos.size" />'>&nbsp;</td>
              <td rowspan='<s:property value="#detail.orderInfos.size" />'><s:property value="#detail.productRootCategory" /></td>
              <td rowspan='<s:property value="#detail.orderInfos.size" />'><s:property value="#detail.productCategory" /></td>
              <td rowspan='<s:property value="#detail.orderInfos.size" />'><s:property value="#detail.productName" /></td>
              <td rowspan='<s:property value="#detail.orderInfos.size" />'><s:property value="#detail.productType" /></td>
              <td rowspan='<s:property value="#detail.orderInfos.size" />'><s:property value="#detail.productSku" /></td>
              <td rowspan='<s:property value="#detail.orderInfos.size" />'><s:property value="#detail.unit" /></td>
              <td rowspan='<s:property value="#detail.orderInfos.size" />'><s:property value="#detail.acceptNumber" /></td>
              <td><s:textfield id="planNums-%{#st.index}-%{#ost.index}" name="planNums" size="10" 
					cssClass="{required:true,maxlength:10,number:true}" /></td>
	          <td><s:textfield id="batchNos-%{#st.index}-%{#ost.index}" name="batchNos" size="10" cssClass="{maxlength:10}"/></td>
              <td><s:property value="#order.orderId" /></td>
              <td><s:property value="#order.number" /></td>
              <td><s:date name="#order.finishDate" format="yyyy-MM-dd"/></td>
    		  <td>
                <s:hidden name="productIds" value="%{#detail.productId}" />
                <s:hidden name="rootCategoryNames" value="%{#detail.productRootCategory}" />
                <s:hidden name="categoryNames" value="%{#detail.productCategory}" />
                <s:hidden name="productSkus" value="%{#detail.productSku}" />
                <s:hidden name="productNos" value="%{#detail.productNo}" />
              </td>
	        </s:if>
	        <s:else>
              <td><s:textfield id="planNums-%{#st.index}-%{#ost.index}" name="planNums" size="10" 
					cssClass="{required:true,maxlength:10,number:true}" /></td>
	          <td><s:textfield id="batchNos-%{#st.index}-%{#ost.index}" name="batchNos" size="10" cssClass="{maxlength:10}"/></td>
              <td><s:property value="#order.orderId" /></td>
              <td><s:property value="#order.number" /></td>
              <td><s:date name="#order.finishDate" format="yyyy-MM-dd"/></td>
    		  <td>
                <s:hidden name="productIds" value="%{#detail.productId}" />
                <s:hidden name="rootCategoryNames" value="%{#detail.productRootCategory}" />
                <s:hidden name="categoryNames" value="%{#detail.productCategory}" />
                <s:hidden name="productSkus" value="%{#detail.productSku}" />
                <s:hidden name="productNos" value="%{#detail.productNo}" />
              </td>
	        </s:else>
	        </tr>
	      </s:iterator>
	    </s:iterator>
        <tr>
         <td colspan="12" align="left" valign="top">
           <input type="button" value="添加" class="input1" onclick="addLine()" />
         </td>
        </tr>
       </table>
      </div>
     <div class="clear"></div>
	 <div class="main_long">
		<table width="98%" border="0" cellpadding="5" cellspacing="0" class="gr-text1">
		   <tr>
			 <td width="45%" align="right">
			   <input name="button" type="button" class="input1" value="保存" onclick="saveSubmit()"/>&nbsp;&nbsp;
			   <input type="button" value="关闭" class="input1" onclick='javascript:window.location.href="planDetailInfo.action"' /></td>
			 <td width="55%" align="left"></td>
		   </tr>
	   </table>
	</div>
	<div class="clear"></div>
	</s:form>
</BODY></HTML>
